package com.js.service.bot.response;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.js.common.annotation.ResponseClass;
import com.js.common.annotation.ResponseCommand;
import com.js.common.core.BotHandler;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import net.mamoe.mirai.utils.ExternalResource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.util.*;

@Service
@Scope("prototype")
@ResponseClass("wiki")
public class WikiService extends BotHandler {

    @ResponseCommand(
            value = "#今日素材",
            name = "今日素材"
    )
    public MessageChain todayMaterial(Long userId, Long targetId, String command, MessageChain messageChain) throws Exception {
        MessageChainBuilder result = new MessageChainBuilder();
        int week = DateUtil.thisDayOfWeek();
        if(week == 1){
            week = 7;
        }else{
            week = week - 1;
        }
        ExternalResource resource = ExternalResource.create(new ByteArrayInputStream(getMaterial(week)));
        result.append(getEvent().getSubject().uploadImage(resource.toAutoCloseable()));
        return result.build();
    }

    @ResponseCommand(
            value = "#明日素材",
            name = "明日素材"
    )
    public MessageChain tomorrowMaterial(Long userId, Long targetId, String command, MessageChain messageChain) throws Exception {
        MessageChainBuilder result = new MessageChainBuilder();
        int week = DateUtil.dayOfWeek(DateUtil.offsetDay(new Date(), 1));
        if(week == 1){
            week = 7;
        }else{
            week = week - 1;
        }
        ExternalResource resource = ExternalResource.create(new ByteArrayInputStream(getMaterial(week)));
        result.append(getEvent().getSubject().uploadImage(resource.toAutoCloseable()));
        return result.build();
    }

    public byte[] getMaterial(int week){
        String response = HttpUtil.get("https://api-static.mihoyo.com/common/blackboard/ys_obc/v1/get_activity_calendar?app_sn=ys_obc");
        JSONObject obj = JSONUtil.parseObj(response);
        JSONArray list = obj.getJSONObject("data").getJSONArray("list");
        List<List<Map>> materials = new ArrayList<>();
        Map<String, Integer> index = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            JSONObject material = list.getJSONObject(i);
            if("2".equals(material.getStr("kind"))){
                String[] dropDay = material.getJSONArray("drop_day").toBean(String[].class);
                if(ArrayUtil.contains(dropDay, String.valueOf(week))){
                    JSONArray contentInfos = material.getJSONArray("contentInfos");
                    JSONObject contentInfo = contentInfos.getJSONObject(contentInfos.size() - 1);
                    if("早柚".equals(material.getStr("title"))){
                        contentInfo = contentInfos.getJSONObject(1);
                    }
                    String title = contentInfo.getStr("title");
                    if(!index.containsKey(title)){
                        index.put(title, materials.size());
                        materials.add(new ArrayList<>());
                        Map<String, String> value = new HashMap<>();
                        value.put("title", title);
                        value.put("img_url", contentInfo.getStr("icon"));
                        materials.get(index.get(title)).add(value);
                    }
                    Map<String, String> value = new HashMap<>();
                    value.put("title", material.getStr("title"));
                    value.put("img_url", material.getStr("img_url"));
                    materials.get(index.get(title)).add(value);
                }
            }
        }
        Integer max = index.values().stream().reduce(0, Integer::max);
        Map<String, Object> params = new HashMap<>();
        params.put("max", max);
        params.put("materials", materials);
        params.put("dir", "break");
        params.put("tpl", "break");
        byte[] bytes = HttpRequest.post("http://127.0.0.1:7001/template").body(JSONUtil.toJsonStr(params)).execute().bodyBytes();
        return bytes;
    }
}
